class_name UICardItem
extends Control

signal signal_mouse_enter
signal signal_mouse_exit
var is_mouse_hover: bool = false

@onready var card_img: TextureRect = $CardImg
@onready var selected_effect: ColorRect = $SelectedEffect
@onready var selecting_effect: ColorRect = $SelectingEffect

var item_data: ItemData = null


func _process(delta: float) -> void:
	process_mouse_hover()
	

func init_by_data(data: ItemData):
	item_data = data
	card_img.texture = ResourceManager.get_resource(data.item_config.icon_name)
	
	
func set_selecting():
	selecting_effect.visible = true


func set_unselecting():
	selecting_effect.visible = false
	

func set_selected():
	selected_effect.visible = true
	

func set_unselected():
	selected_effect.visible = false

	
func process_mouse_hover():
	var mouse_position = get_viewport().get_mouse_position()
	var bounds = Rect2(global_position, card_img.size)
	
	if mouse_position.x >= bounds.position.x and \
	mouse_position.x <= bounds.position.x + bounds.size.x and \
	mouse_position.y >= bounds.position.y and \
	mouse_position.y <= bounds.position.y + bounds.size.y:
		if not is_mouse_hover:
			is_mouse_hover = true
			signal_mouse_enter.emit(self)
	else:
		if is_mouse_hover:
			is_mouse_hover = false
			signal_mouse_exit.emit(self)
			
